#!/usr/bin/python3
# -*- coding:utf-8 -*-
# __author__ == taoyulong2018@gmail.com
# __time__ == 2023/8/18 16:14
# ===========================================
#       题目名称： 13. 罗马数字转整数
#       题目地址： https://leetcode.cn/problems/roman-to-integer/description/
#       题目描述： https://note.youdao.com/s/RRXNSXjR
# ===========================================


class Solution:
    """
        实现思路：
            定义左右指针，从字符串从后往前找，找到合适的就干掉 知道字符串为空
    """

    def romanToInt(self, s: str) -> int:
        if not 1 <= len(s) <= 15:
            return 0
        roman_dict = {"I": 1, "II": 2, "III": 3, "IV": 4, "V": 5, "VI": 6, "VII": 7, "VIII": 8, "IX": 9,
                      "X": 10, "XX": 20, "XXX": 30, "XL": 40, "L": 50, "LX": 60, "LXX": 70, "LXXX": 80, "XC": 90,
                      "C": 100, "CC": 200, "CCC": 300, "CD": 400, "D": 500, "DC": 600, "DCC": 700, "DCCC": 800, "CM": 900,
                      "M": 1000, "MM": 2000, "MMM": 3000,
                      }
        res = 0
        # 先对字符串进行切割
        while s:
            left, right = 0, len(s)  # 定义双指针
            while left < right:
                if s[left: right] in roman_dict.keys():
                    res += roman_dict[s[left: right]]
                    break
                else:
                    right -= 1
            s = s[right:]
        return res


if __name__ == '__main__':
    s = Solution()
    print("3 =>", s.romanToInt("III"))
    print("4 =>", s.romanToInt("IV"))
    print("9 =>", s.romanToInt("IX"))
    print("58 =>", s.romanToInt("LVIII"))
    print("1994 =>", s.romanToInt("MCMXCIV"))
